
/* 
 * com.ctvit.framework.web.listener.ContextLoadListener.java Create on Apr 18, 2008 3:53:29 PM By Q-Wang 
 * $HEAD$
 */
package com.ctvit.framework.web.listener;

import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ctvit.framework.util.DateConverter;
import com.ctvit.framework.web.context.ContextHolder;
import com.ctvit.sms.platform.service.FileTaskService;
import com.ctvit.sms.platform.service.MsgTaskService;
import com.ctvit.sms.platform.service.SmsService;
import com.ctvit.struts.common.CommonFunc;

/**
 * ServletContext Life Cycle Listener.
 * 
 * @author Q-Wang
 * 
 */
public class ContextLoadListener implements ServletContextListener {

	protected final Log log = LogFactory.getLog(ContextLoadListener.class);

	protected final boolean isDebugEnabled = log.isDebugEnabled();
	
	protected ServletContext servletContext;
	
	protected ApplicationContext applicationContext;

	/**
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent event) {
		ContextHolder.getInstance().shutdown();
		log.debug("ContextLoadListener: Context Destroyed.");
	}

	/**
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent event) {
		if (isDebugEnabled) {
			log.debug("ContextLoadListener: Context Loading...");
		}
		servletContext = event.getServletContext();
		applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		doInitialized();
		if (isDebugEnabled) {
			log.debug("ContextLoadListener: Context Initialize Success.");
		}
		if (CommonFunc.startTimer.equals("1")){
//			ReqTaskService reqTaskService=(ReqTaskService)ContextHolder.getBean(ReqTaskService.class);
//			if (reqTaskService!=null) reqTaskService.startTimer();
			FileTaskService fileTaskService=(FileTaskService)ContextHolder.getBean(FileTaskService.class);
			if (fileTaskService!=null) fileTaskService.startTimer();
		}
//		SmsService smsService=(SmsService)ContextHolder.getBean(SmsService.class);
//		if (smsService!=null){
//			if ("1".equals(CommonFunc.synAllPerson)){
//				smsService.insertAllPersons();
//				smsService.insertAllDepts();
//			}
//		}
	}

	protected void doInitialized() {
		//registerConverters();
		registerServletContext();
	}
	
	protected void registerConverters() {
		ConvertUtils.register(new IntegerConverter(null), Integer.class);
		ConvertUtils.register(new LongConverter(null), Long.class);
		ConvertUtils.register(new FloatConverter(null), Float.class);
		ConvertUtils.register(new DoubleConverter(null), Double.class);
		ConvertUtils.register(new DateConverter("yyyy-MM-dd"), Date.class);
	}

	protected void registerServletContext() {
		ContextHolder.getInstance().setServletContext(servletContext);
		ContextHolder.getInstance().setApplicationContext(applicationContext);
	}
}
